package model

import (
	// "fmt"
	// "database/sql"
	. "github.com/klen-ygs/gorm-zero/gormc/sql"
	"gorm.io/gorm"
	"context"
	"github.com/klen-ygs/gorm-zero/gormc"
)

// avoid unused err
var _ = InitField
var _ TypesModel = (*customTypesModel)(nil)

type (
	// TypesModel is an interface to be customized, add more methods here,
	// and implement the added methods in customTypesModel.
	TypesModel interface {
		typesModel
		customTypesLogicModel
	}

	customTypesLogicModel interface {
		WithSession(tx *gorm.DB) TypesModel
		Search(ctx context.Context, limit int, offset int) ([]Types, int32, error)
	}

	customTypesModel struct {
		*defaultTypesModel
	}
)

func (c customTypesModel) WithSession(tx *gorm.DB) TypesModel {
	newModel := *c.defaultTypesModel
	c.defaultTypesModel = &newModel
	c.conn = tx
	return c
}

// NewTypesModel returns a model for the database table.
func NewTypesModel(conn *gorm.DB) TypesModel {
	return &customTypesModel{
		defaultTypesModel: newTypesModel(conn),
	}
}

func (m *defaultTypesModel) customCacheKeys(data *Types) []string {
	if data == nil {
		return []string{}
	}
	return []string{}
}

func (m *defaultTypesModel) Search(ctx context.Context, limit int, offset int) ([]Types, int32, error) {
	var resp []Types
	var count int64
	filter := Types{
				Removed:false,
			}
	err := m.conn.WithContext(ctx).Model(&Types{}).Where(&filter).Count(&count).Offset(offset).Limit(limit).Find(&resp).Error
	// err := m.conn.WithContext(ctx).Model(&Types{}).Count(&count).Offset(offset).Limit(limit).Take(&resp).Error
	if err == gormc.ErrNotFound {
		return nil, 0, err
	}
	// ret := make([]Dict, len(resp))

	// fmt.Printf("[%d]%+v\n",len(resp), resp)

	// for i,item := range resp {
	// 	ret[i] = Dict{
	// 		"id":         item.Id,
	// 		"name":       item.Name,
	// 		"detail":     item.Detail,
	// 		"status":     item.Status,
	// 		// "removed":    item.Removed,
	// 		"createtime": item.CreateTime.Format("2006-01-02 15:04:05"),
	// 	}
	// 	// fmt.Printf("[%d]%+v\n", i, ret[i])
	// }
	return resp, int32(count), err
}

